import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:
 * Date: 2024-09-24
 * Time: 8:22
 */
public class MyArrayList implements IList{
    public int[] array;
    public int useSize;
    public static final int DEAULT_CAPACITY = 10;

    public MyArrayList(){
        this.array = new int[DEAULT_CAPACITY];
    }


    @Override
    public void add(int data) {
        if(isFull()){
            grow();
        }
        this.array[this.useSize] = data;
        useSize++;
    }

    private boolean isFull(){
        return array.length == useSize;
    }

    private void grow(){
        this.array = Arrays.copyOf(array, 2*array.length);
    }

    @Override
    public void add(int pos, int data) {
        try{
            checkPos(pos);
        }catch (PosIIIegal e){
            System.out.println(" 插入元素pos位置不合法");
            e.printStackTrace();
        }

        if(isFull()){
            grow();
        }

        for (int i = useSize-1; i >= pos ; i--) {
            array[i+1] = array[i];
        }

        array[pos] = data;
        useSize++;
    }

    private void checkPos(int pos) throws PosIIIegal{
        if(pos < 0 || pos > useSize){
            throw new PosIIIegal("pos位置不合法");
        }
    }

    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == toFind){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    @Override
    public int get(int pos) {
        try{
            checkEmpty();
            checkPos2(pos);
            return array[pos];
        }catch (PosIIIegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }

        return -1;
    }

    private void checkEmpty() throws EmptyException {
        if(isEmpty()){
            throw new EmptyException("顺序表为空");
        }
    }

    private void checkPos2(int pos) throws PosIIIegal{
        if(pos < 0 || pos >= this.useSize){
            throw new PosIIIegal("Pos位置不合法！");
        }
    }

    public boolean isEmpty(){
        return useSize == 0;
    }

    @Override
    public void set(int pos, int value) {
        try{
            checkEmpty();
            checkPos2(pos);
            array[pos] = value;
        }catch (PosIIIegal e){
            e.printStackTrace();
        }catch (EmptyException e){
            e.printStackTrace();
        }

        return ;
    }


    @Override
    public void remove(int toRemove) {
        try{
            checkEmpty();
            int pos = indexOf(toRemove);
            if(pos == -1){
                return;
            }
            for (int i = useSize-1; i >= pos ; i--) {
                array[i+1] = array[i];

            }
            useSize--;
        }catch (EmptyException e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return useSize;
    }

    @Override
    public void clear() {
        this.useSize = 0;
    }

    @Override
    public void display() {
        for (int i = 0; i < this.useSize; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public void searchAdd(int val){
        if(val < array[0]){
            add(0,val);
        }
        if(val > array[useSize-1]){
            add(val);
        }
        int i = 0;
        for (; i < array.length; i++) {
            if(array[i] < val && array[i+1] > val){
                break;
            }
        }
        add(i+1, val);
    }

    public void Josephus(int n, int start, int distance){

    }
}
